package com.zerdoor.controller;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.zerdoor.model.TaskType;
import com.zerdoor.pagehelper.PageHelper;
import com.zerdoor.pagehelper.PageInfo;
import com.zerdoor.service.PlatSetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Project: acgplat
 * Module ID: 011
 * Comments: 任务分类管理
 * JDK version used: JDK1.7
 * Namespace: controller
 * Author：xiaobo
 * Create Date：2014-11-24
 * Modified By：   <修改人中文名或拼音缩写>
 * Modified Date:  <修改日期，格式:YYYY-MM-DD>
 * Why & What is modified  <修改原因描述>
 * Version:                  <版本号>
 */
@Controller
@RequestMapping("plat")
public class PlatController {

    @Autowired
    private PlatSetService platSetService;

    @RequestMapping(value = "/index",method = RequestMethod.GET)
    public String index(Model model){
        model.addAttribute("list",platSetService.getTaskTypeSkill());
        return "/plat/view";
    }

    /**
     * 查询树形列表（任务分类）
     * @param id 任务父类编号
     * @param request
     * @param page 当前页
     * @param rows 页大小
     * @return
     */
    @RequestMapping(value = "view", method = RequestMethod.POST)
    @ResponseBody
    public String view(int id,HttpServletRequest request,int page,int rows){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("id",id);
        if(id == 1){
            PageHelper.startPage(page, rows);
            List<Map<String, Object>> list = platSetService.getTaskTypeInfo(map);
            PageInfo pageInfo = new PageInfo(list);
            Gson gson = new Gson();
            Map<String,Object> obj = new HashMap<String, Object>();
            obj.put("rows", getJson(list));
            obj.put("total", pageInfo.getTotal());
            String result = gson.toJson(obj);
            return result;
        }else{
            //添加子节点
            List<Map<String, Object>> list = platSetService.getTaskTypeInfo(map);
            Gson gson = new Gson();
            return gson.toJson(getJson(list));
        }
    }

    /**
     * 初始化list列表
     * @param list
     * @return
     */
    public List<TaskType> getJson(List<Map<String, Object>> list){
        List<TaskType> list2 = new ArrayList<TaskType>();
        for (Map<String, Object> temp : list){
            TaskType t = new TaskType();
            t.setId(Integer.parseInt(temp.get("id").toString()));
            t.setNameCn(temp.get("nameCn").toString());
            t.setNameEn(temp.get("nameEn").toString());
            t.setSort(Integer.parseInt(temp.get("sort").toString()));
            t.setSTATUS(Integer.parseInt(temp.get("STATUS").toString()));
            t.setSkill(temp.get("skill").toString());
            t.setSkillName(temp.get("skillName").toString());
            list2.add(t);
        }
        return  list2;
    }

    /**
     * 添加分类
     * @param request
     * @return
     */
    @RequestMapping(value = "addType", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> addType(HttpServletRequest request){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("tasktype_id", null);
        map.put("nameCn", request.getParameter("nameCn"));
        map.put("nameEn", request.getParameter("nameEn"));
        map.put("parent", Integer.parseInt(request.getParameter("parent")));
        map.put("sort", Integer.parseInt(request.getParameter("sort")));
        map.put("tasktypeStatus", Integer.parseInt(request.getParameter("tasktypeStatus")));
        map.put("skill", request.getParameter("skill"));
        try {
            platSetService.addTaskType(map);
        } catch (Exception e) {
            map.put("errormsg",e.getMessage());
        }
        return map;
    }

    /**
     * 编辑分类
     * @param request
     * @return
     */
    @RequestMapping(value = "editType", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> editType(HttpServletRequest request){
        Map<String,Object> map = new HashMap<String,Object>();

        map.put("tasktypeId", Integer.parseInt(request.getParameter("tasktypeId")));
        map.put("nameCn", request.getParameter("nameCn"));
        map.put("nameEn", request.getParameter("nameEn"));
        map.put("sort", Integer.parseInt(request.getParameter("sort")));
        map.put("tasktypeStatus", Integer.parseInt(request.getParameter("tasktypeStatus")));
        map.put("skill", request.getParameter("skill"));
        try {
            platSetService.updateTaskType(map);
        } catch (Exception e) {
            map.put("errormsg",e.getMessage());
        }
        return map;
    }

    /**
     * 删除分类
     * @param request
     * @return
     */
    @RequestMapping(value = "deleteType", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> deleteType(HttpServletRequest request,int tasktypeId){
        Map<String,Object> map = new HashMap<String,Object>();
        try {
            platSetService.deleteTaskType(tasktypeId);
        } catch (Exception e) {
            map.put("errormsg",e.getMessage());
        }
        return map;
    }


}
